package com.ryskoo.controller.cms;

import com.ryskoo.cms.notice.domain.NoticePO;
import com.ryskoo.cms.notice.service.INoticeService;
import com.ryskoo.cms.noticetype.domain.NoticeTypePO;
import com.ryskoo.cms.noticetype.service.INoticeTypeService;
import com.ryskoo.common.constant.CmsParamConstant;
import com.ryskoo.framework.core.bean.QueryMessageDTO;
import com.ryskoo.framework.core.bean.ReturnMessageDTO;
import com.ryskoo.framework.core.bean.SimpleDataParam;
import com.ryskoo.framework.core.controller.BasicHandler;
import com.ryskoo.framework.core.utils.CommonUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

import java.util.List;

/**
 * 公告信息管理 web controller
 *
 * @author renyuting
 * @data 2018-05-05
 */
@Controller
@RequestMapping(value = "/vendor/cms/notice")
public class NoticeController extends BasicHandler<NoticePO> {

    private INoticeService noticeService;

    @Autowired
    private INoticeTypeService noticeTypeService;

    public NoticeController() {
        super("system/cms/notice", "Notice");
    }

    @Autowired
    public void setService(INoticeService noticeService) {
        this.noticeService = noticeService;
        super.setService(noticeService);
    }

    @Override
    public ReturnMessageDTO list(@RequestBody QueryMessageDTO queryMessage) {
        //展示公告类型数据
        ReturnMessageDTO<List<NoticePO>> noticeReturnMessage = new ReturnMessageDTO<>();
        List<NoticePO> noticeList = noticeService.queryByCondition(queryMessage);
            if(noticeList !=null && noticeList.size()>0){
                for (NoticePO notice:noticeList ) {
                    NoticeTypePO noticeType = noticeTypeService.getByUuid(notice.getNoticeTypeUuid());
            		if(noticeType != null){
                        notice.setNoticeTypeName(noticeType.getTypeName());
                    }
                }
            }
        noticeReturnMessage.setReturnMessage(noticeList);
        return noticeReturnMessage;
    }

    @Override
    protected void buildAddPageData(Model model, HttpServletRequest request) {
        //展示公告类型数据
        QueryMessageDTO queryMessage = new QueryMessageDTO();
        List<NoticeTypePO> noticeTypeList = noticeTypeService.queryByCondition(queryMessage);
        model.addAttribute("noticeTypeList",noticeTypeList);
        //展示公告状态
        List<SimpleDataParam> stateList = CommonUtil.converseSimpleDataParamMap(CmsParamConstant.getNoticeStateMap(), false);
        model.addAttribute("stateList",stateList);

        //展示接收客户
        List<SimpleDataParam> noticeCustomerList = CommonUtil.converseSimpleDataParamMap(CmsParamConstant.getNoticeCustomerMap(), false);
        model.addAttribute("noticeCustomerList",noticeCustomerList);
        super.buildAddPageData(model,request);
    }

    @Override
    protected void buildEditPageData(Model model, NoticePO dto, HttpServletRequest request) {
        //展示公告类型数据
        QueryMessageDTO queryMessage = new QueryMessageDTO();
        List<NoticeTypePO> noticeTypeList = noticeTypeService.queryByCondition(queryMessage);
        model.addAttribute("noticeTypeList",noticeTypeList);
        //展示公告状态
        List<SimpleDataParam> stateList = CommonUtil.converseSimpleDataParamMap(CmsParamConstant.getNoticeStateMap(), false);
        model.addAttribute("stateList",stateList);

        //展示接收客户
        List<SimpleDataParam> noticeCustomerList = CommonUtil.converseSimpleDataParamMap(CmsParamConstant.getNoticeCustomerMap(), false);
        model.addAttribute("noticeCustomerList",noticeCustomerList);
        super.buildEditPageData(model, dto, request);
    }
}
